package com.example.hybrid;

import android.content.Context;
import android.content.Intent;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.example.WalletApplication;
import com.example.activity.SharePageActivity;
import com.example.model.EventBean;
import com.example.util.AppInfoUtil;
import com.example.util.Constant;
import com.example.util.PayUtil;
import com.example.util.SharePreferenceUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * JS接口调用
 */
public class JsBridge {
    private Context mContext;

    public JsBridge(Context context) {
        this.mContext = context;
    }

    @JavascriptInterface
    public void payByWeChat(String orderInfo){
        PayUtil.payByWechat(mContext,orderInfo);


    }

    @JavascriptInterface
    public void payByAlipay(String orderInfo){
        PayUtil.payByAli(mContext,orderInfo);
    }

    @JavascriptInterface
    public void saveValue(String key,String value){
        SharePreferenceUtils.putString(WalletApplication.getInstance(),key,value);
    }

    @JavascriptInterface
    public String getValue(String key){
        return SharePreferenceUtils.getString(WalletApplication.getInstance(),key,"");
    }

    @JavascriptInterface
    public void deleteValue(String key){
        SharePreferenceUtils.removeValue(WalletApplication.getInstance(),key);
    }

    @JavascriptInterface
    public void dealLogin(){
        EventBean eventBean = new EventBean();
        eventBean.setTag(Constant.EVENT_LOGIN);
        EventBus.getDefault().post(eventBean);
    }

    @JavascriptInterface
    public void dealLoginOut(){
        EventBean eventBean = new EventBean();
        eventBean.setTag(Constant.EVENT_LOGIN_OUT);
        EventBus.getDefault().post(eventBean);
    }

    @JavascriptInterface
    public void dealShare(String inviteCode,String url){
        Intent intent = new Intent(mContext, SharePageActivity.class);
        intent.putExtra("code",inviteCode);
        intent.putExtra("url",url);
        mContext.startActivity(intent);
    }

}
